package states;

import interfaces.StateController;

import java.util.ArrayList;

import objects.*;
import ect.GeneralPause;
import globals.*;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import buttons.ClickButton;

public class Bounce extends BasicGameState {

	StateController sController;
	public Input input; // key imput
	GeneralPause gPause;
	
	ClickButton button1;
	ArrayList<Collidable> collidingObjects;
	objects.SimpleBallObject ball1, ball2, ball3, ball4;
	Wall wall1, wall2, wall3;
	
	private int id;
	Bounce(int stateId){
		this.id=stateId;
		
		this.id=stateId;
		sController=new StateController();
		sController.setDelay(3*SlickGame.fps);
		gPause=new GeneralPause("Bounce",id);
		gPause.isPaused=true;
		
		button1=new ClickButton(100,100,70,70,"Pause");
		ball1=new SimpleBallObject(SlickGame.width/2-10,SlickGame.height/2,4, Color.blue);
		ball2=new SimpleBallObject(SlickGame.width/2-5,SlickGame.height/2,4, Color.red);
		ball3=new SimpleBallObject(SlickGame.width/2+5,SlickGame.height/2,4, Color.green);
		ball4=new SimpleBallObject(SlickGame.width/2+10,SlickGame.height/2,4, Color.yellow);
		wall1=new Wall(SlickGame.width/2,SlickGame.height-20,80, Color.white);
		collidingObjects=new ArrayList<Collidable>();
		collidingObjects.add(ball1);
		collidingObjects.add(ball2);
		collidingObjects.add(ball3);
		collidingObjects.add(ball4);
		collidingObjects.add(wall1);
	}
	
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		gPause.init(gc, sbg);
	}

	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		g.setBackground(Color.black);
		
		ball1.draw(g);
		ball2.draw(g);
		ball3.draw(g);
		ball4.draw(g);
		wall1.draw(g);
		
		button1.draw(g);
		
		if(button1.isClicked()){
			g.setColor(ball1.selfColor.darker((float) 0.8));
			g.draw(new Rectangle(ball1.x,0,3,SlickGame.height));
			g.setColor(ball2.selfColor.darker((float) 0.8));
			g.draw(new Rectangle(ball2.x,0,3,SlickGame.height));
			g.setColor(ball3.selfColor.darker((float) 0.8));
			g.draw(new Rectangle(ball3.x,0,3,SlickGame.height));
			g.setColor(ball4.selfColor.darker((float) 0.8));
			g.draw(new Rectangle(ball4.x,0,3,SlickGame.height));
			
			g.setColor(Color.white);
			g.drawString("1", ball1.x, ball1.y-20);
			g.drawString("2", ball2.x, ball2.y-20);
			g.drawString("3", ball3.x, ball3.y-20);
			g.drawString("4", ball4.x, ball4.y-20);
		}
		g.setColor(Color.white);
		g.drawString("X: "+Mouse.getX()+":"+SlickGame.width+" Y: "+(SlickGame.height-Mouse.getY())+":"+SlickGame.height+" C: "+Mouse.isButtonDown(0), 10, 30);
		g.drawString("Ball 1:\n"+(int)ball1.x+":"+(int)ball1.y+"\nVel:"+ball1.direction.getScalar(), 75, SlickGame.height-60);
		g.drawString("Ball 2:\n"+(int)ball2.x+":"+(int)ball2.y+"\nVel:"+ball2.direction.getScalar(), 280, SlickGame.height-60);
		g.drawString("Ball 3:\n"+(int)ball3.x+":"+(int)ball3.y+"\nVel:"+ball3.direction.getScalar(), SlickGame.width-350, SlickGame.height-60);
		g.drawString("Ball 4:\n"+(int)ball4.x+":"+(int)ball4.y+"\nVel:"+ball4.direction.getScalar(), SlickGame.width-150, SlickGame.height-60);
		
		g.setColor(Color.gray.darker());
		g.drawString("Bounce",SlickGame.width/2-20,20);
		
		if(gPause.isPaused){
			gPause.draw(g);
		}
	}

	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		this.input = gc.getInput();
		sController.update(input, delta);
		
		if(!gPause.isPaused){
			ball1.update(collidingObjects);
			ball2.update(collidingObjects);
			ball3.update(collidingObjects);
			ball4.update(collidingObjects);
			wall1.update(collidingObjects);
		}
		else{
			gPause.update(sbg, delta);
		}
		
		if(sController.key_lShift){
			sController.key_lShift=false;
			gPause.pauseState=gPause.playState;
			gPause.togglePause();
		}
		
		button1.update(Mouse.getX(), (SlickGame.height-Mouse.getY()), Mouse.isButtonDown(0), delta);
	}
	
	public static void destroy(){
		
	}

	public int getID() {
		return this.id;
	}


}
